package me.jessyan.mvparms.demo.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.session.MediaSession;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.platform.ai.control.R;



import butterknife.BindView;
import butterknife.OnClick;

import me.jessyan.mvparms.demo.di.component.DaggerUserComponent;
import me.jessyan.mvparms.demo.di.module.UserModule;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import me.jessyan.mvparms.demo.mvp.model.logic.TextUtil;
import me.jessyan.mvparms.demo.mvp.presenter.UserPresenter;
import me.jessyan.mvparms.demo.util.SmsCount;


// 注册界面
public class UserMessageLoginActivity extends BaseActivity<UserPresenter> implements UserContract.View {
    @BindView(R.id.activity_register)
    View mContent;

    @BindView(R.id.tv_login_pwd)
    TextView mLoginByPwd;

    @BindView(R.id.tv_login_code)
    TextView mLoginByCode;

    @BindView(R.id.login)
    View mLoginLayout;

    @BindView(R.id.sms_login)
    View mSmsLoginLayout;

    @BindView(R.id.et_phone)
    EditText mPhone;

    @BindView(R.id.et_password)
    EditText mPassword;

    @BindView(R.id.et_sms_phone)
    EditText mSmsPhone;

    @BindView(R.id.et_sms_code)
    EditText mSmsCode;

    @BindView(R.id.tv_sms_code)
    TextView mAuthCode;



    public static int isCodeLog = 0;  // 0 密码登录 1 验证码登录 2 微信登录



    private int flag = 3;  //0 密码登录 1验证码登录  2注册 3 找回密码
    public static boolean LOGIN_SUCCESS = false;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerUserComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .userModule(new UserModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        return R.layout.activity_user_message_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        //清空緩存資料
        LOGIN_SUCCESS = false;
        SharedPreferences sharedPreferences = getSharedPreferences("UserParam",this.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();

//        thrid_auth.setActivity(this,mPresenter);
    }


    @Override
    public void showLoading() {

    }


    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        //checkNotNull(message);
        //ArmsUtils.snackbarText(message);
        if (!message.isEmpty()) {
            Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
//        int code = intent.getIntExtra(KeyDefine.RESULT_CODE,-1);
//        switch(code){
//            case KeyDefine.GET_MORE_RESULT:
//                Intent next = new Intent(this, UserRegisterActivity.class);
//                next.putExtra("type","auth_reg");
//                next.putExtra("authinfo",intent.getSerializableExtra("authinfo"));
//                startActivityForResult(next, 1000);
//                break;
//
//            // 验证码发送成功
//            case KeyDefine.GET_ORTHER:
//
//                MobclickAgent.onEvent(UserMessageLoginActivity.this, "get_yzm_success");
//                initTimer();
//                mAuthCode.setBackgroundResource(R.drawable.bg_arc_btn_gray);
//                mAuthCode.setTextColor(Color.parseColor("#ffffff"));
//                mAuthCode.setEnabled(false);
//                break;
//        }

    }

    @Override
    public void killMyself() {
        finish();
    }






    @Override
    public void setAdapter(DefaultAdapter adapter) {

    }

    @Override
    public void loginReturn(User user) {

    }

    @Override
    public void logout() {

    }



    public void uploadResult(String string) {

    }

//    private long mPressedTime;
//    @OnClick(R.id.iv_back)
//    public void back(View v){
//        long mNowTime = System.currentTimeMillis();//获取第一次按键时间
//        if ((mNowTime - mPressedTime) > 2000) {//比较两次按键时间差
//            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
//            mPressedTime = mNowTime;
//        } else {//退出程序
//            this.finish();
//            System.exit(0);
//        }
//    }

    @OnClick(R.id.tv_forget)
    public void forgetPwd(View v){
//        Toast.makeText(this, "忘记密码", Toast.LENGTH_SHORT).show();
//        ArmsUtils.startActivity(UserFindPwdActivity.class);
    }



    @OnClick(R.id.btn_login)
    public void login(View view){

        String phone = mPhone.getText().toString().trim();
        String password = mPassword.getText().toString().trim();
//        if(TextUtil.isEmpty(phone)){
//            showMessage("手机号不能为空");
//            return;
//        }else if(!TextUtil.isMobile(phone)){
//            showMessage("手机号码非法");
//            return;
//        }else if(TextUtil.isEmpty(password)){
//            showMessage("密码不能为空");
//            return;
//        }
//        else if(password.length()< 6 || password.length()> 20 ){
//            showMessage("密码非法");
//            return;
//        }
        ArmsUtils.startActivity(MainActivity.class);
    }

    @OnClick(R.id.btn_sms_login)
    public void loginBySms(View view){
        String phone = mSmsPhone.getText().toString().trim();
        String code = mSmsCode.getText().toString().trim();
        if(TextUtil.isEmpty(phone)){
            showMessage("手机号不能为空");
            return;
        }else if(!TextUtil.isMobile(phone)){
            showMessage("手机号码非法");
            return;
        }else if(TextUtil.isEmpty(code)){
            showMessage("验证码不能为空");
            return;
        }else if(code.length() != 6){
            showMessage("验证码非法");
            return;
        }

    }

    @OnClick(R.id.tv_login_pwd)
    public void tabPwdLogin(View v){
        isCodeLog = 0;
        mLoginByPwd.setAlpha(1f);
        mLoginByCode.setAlpha(0.5f);
        mLoginLayout.setVisibility(View.VISIBLE);
        mSmsLoginLayout.setVisibility(View.GONE);
    }

    @OnClick(R.id.tv_login_code)
    public void tabCodeLogin(View v){
        isCodeLog = 1;
        mLoginByPwd.setAlpha(0.5f);
        mLoginByCode.setAlpha(1f);
        mLoginLayout.setVisibility(View.GONE);
        mSmsLoginLayout.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.ll_register)
    public void toRegister(View v){
//        Intent intent = new Intent(this,UserRegisterActivity.class);
//        startActivityForResult(intent,1000);
    }

    @OnClick(R.id.tv_sms_code)
    public void authcode_btn(View view){
//        if (NoDoubleClickUtils.isDoubleClick()) {
//            return;
//        }
//        String phone = mSmsPhone.getText().toString();
//        if (phone.isEmpty()) {
//            showMessage("请输入手机号码");
//            return;
//        } else if (!TextUtil.isMobile(phone)) {
//            showMessage("请输入正确的手机号码");
//            return;
//        } else {
//            mAuthCode.setFocusable(true);
//            mAuthCode.setFocusableInTouchMode(true);
//            mAuthCode.requestFocus();
//        }
        initTimer();
    }
    @OnClick(R.id.iv_back)
    public void back(View v){
        onBackPressed();
    }

    private long mPressedTime = 0;

    @Override
    public void onBackPressed() {
        long mNowTime = System.currentTimeMillis();//获取第一次按键时间
        if ((mNowTime - mPressedTime) > 2000) {//比较两次按键时间差
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mPressedTime = mNowTime;
        } else {//退出程序
            this.finish();
            System.exit(0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(LOGIN_SUCCESS){
//            String token = Utils.judgeToken(this);
//            mPresenter.getInvestorAuth(token,mContent,mPresenter);
        }

//        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }



    private SmsCount smsCount;
    private void initTimer() {
        smsCount = new SmsCount(mAuthCode,60 * 1000, 100);
        smsCount.start();
    }



}
